﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Phone
{
    //地址簿界面
    public partial class Addresses : Form
    {
        //默认构造函数
        public Addresses()
        {
            InitializeComponent();
        }

        //窗体加载时读取地址列表并显示
        private void Addresses_Load(object sender, EventArgs e)
        {
            for (int i = 0; i < Vars.address_count; i++)
            {
                listBox1.Items.Add(Vars.address[i].address);
            }
        }

        //窗体关闭时退出程序
        private void Addresses_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Form.ActiveForm == this) Application.Exit();
        }

        //返回设置界面
        private void button1_Click(object sender, EventArgs e)
        {
            Settings settings = new Settings();
            settings.Show();
            settings.Location = this.Location;
            this.Close();
        }

        //进入地址信息编辑界面（添加地址信息）
        private void button2_Click(object sender, EventArgs e)
        {
            if (Vars.address_count >= Defines.MAX_ADDRESS_COUNT)
            {
                MessageBox.Show("最多只能添加" + Defines.MAX_ADDRESS_COUNT + "个地址！");
                return;
            }
            AddrInfo addrInfo = new AddrInfo();
            addrInfo.Show();
            addrInfo.Location = this.Location;
            this.Close();
        }

        //删除地址
        private void button3_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null) { return; }
            string address = listBox1.SelectedItem.ToString();
            int n = -1;
            for (int i = 0; i < Vars.address_count; i++)
            {
                if (Vars.address[i].address == address)
                {
                    n = i;
                    break;
                }
            }
            if (n == -1)
            {
                return;
            }
            for (int i = n; i < Vars.address_count - 1; i++)
            {
                Vars.address[i] = Vars.address[i + 1];
            }
            Vars.address[Vars.address_count - 1].gps.jd = -200;
            Vars.address[Vars.address_count - 1].gps.wd = -200;
            Vars.address[Vars.address_count - 1].address = "";
            Vars.address_count--;
            listBox1.Items.Remove(address);
        }

        //双击地址列表项时进入地址信息编辑界面（修改地址信息）
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            {
                return;
            }
            string address = listBox1.SelectedItem.ToString();
            int n = -1;
            for (int i = 0; i < Vars.address_count; i++)
            {
                if (Vars.address[i].address == address)
                {
                    n = i;
                    break;
                }
            }
            if (n == -1)
            {
                return;
            }
            AddrInfo addrInfo = new AddrInfo(n);
            addrInfo.Show();
            addrInfo.Location = this.Location;
            this.Close();
        }
    }
}
